package com.jumper.cwolf.leetcode;

/**
 * 描述：
 *
 * @author cwolf
 * @version 1.0
 * @date 2021/4/2 11:18
 */
public class Code783 {
    //最小值
    int min = Integer.MAX_VALUE;
    //上个节点的值
    Integer f = null;
    public int minDiffInBST(TreeNode root) {
        if(root.left==null&&root.right==null){
            return 0;
        }
        readNode(root);
        return min;
    }

    //按从小到大 取数据
    public void readNode(TreeNode node) {
        TreeNode right = node.right;
        TreeNode left = node.left;
        if(left!=null){
            readNode(left);
        }
        diffNode(node.val);
        if(right!=null){
            readNode(right);
        }
    }
    //计算最小值
    public void diffNode(int s){
        if(f==null){
            f = s;
            return;
        }
        int i = s - f;
        if(i<min){
            min = i;
        }
        f = s;
    }




    public static class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
         this.val = val;
         this.left = left;
         this.right = right;
     }
    }


//        System.out.println("nums:"+JSONUtil.toJsonStr(nums));

    public static void main(String[] args) {

        TreeNode left = new TreeNode(6);
        TreeNode left1 = new TreeNode(8);
        left.right = left1;
        TreeNode right = new TreeNode(10);

        TreeNode right1 = new TreeNode(15);
        TreeNode right2 = new TreeNode(12);

        right2.right = right1;
        right2.left = right;
        TreeNode root = new TreeNode(9);
        root.left = left;root.right = right2;
        int i = new Code783().minDiffInBST(root);
        System.out.println(i);
    }

}
